package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tbContentCategoryController")
public class TbContentCategoryController {

    @Reference
    private TbContentCategoryServiceI tbContentCategoryServiceI;

    private Logger logger= LoggerFactory.getLogger(TbBrandController.class);

    //跳转
    @RequestMapping("getFindAll")
    public String getFindAll(){
        return "admin/content_category";
    }

    //查询
    @RequestMapping("getFindAlls")
    @ResponseBody
    public SpecificPrint getFindAlls(String name){
        try {
            List<TbContentCategory> tbContentCategories = tbContentCategoryServiceI.getFindAlls(name);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbContentCategories);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbContentCategories);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //回显
    @RequestMapping("toUpdate")
    @ResponseBody
    public TbContentCategory toUpdate(Long id){
        return tbContentCategoryServiceI. selectTbContentCategoryById(id);

    }
    //确认修改
    @RequestMapping("updateTbContentCategory")
    @ResponseBody
    public SpecificPrint  updateTbContentCategory(TbContentCategory tbContentCategory){
        try {
            tbContentCategoryServiceI.updateTbContentCategory(tbContentCategory);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbContentCategory);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbContentCategory);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //确认增加
    @RequestMapping("saveTbContentCategory")
    @ResponseBody
    public SpecificPrint  saveTbContentCategory(TbContentCategory tbContentCategory){
        try {
            tbContentCategoryServiceI.saveTbContentCategory(tbContentCategory);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbContentCategory);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbContentCategory);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    @RequestMapping("deleteTbContentCategory")
    @ResponseBody
    public SpecificPrint deleteTbContentCategory(String ids){
        //截取字符串
        String idss=ids.replaceAll("'","");
        String[] split = idss.split(",");
        //遍历出所有选中的ID
        List<Integer> list=new ArrayList<>();
        for (String s : split) {
            list.add(Integer.valueOf(s));
        }
        try {
            tbContentCategoryServiceI.deleteTbContentCategory(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

}
